package com.hurst

import org.joda.time.DateTimeConstants

class WorkWeekController {

    static allowedMethods = [save: "POST", update: "POST", delete: "GET"]

    def pdfRenderingService

    def create = {
        def workWeekInstance = new WorkWeek()
        workWeekInstance.properties = params
        return [workWeekInstance: workWeekInstance]
    }

    def save = {
        def workWeekInstance = new WorkWeek(params)

        if (workWeekInstance.startDate) {

            //TODO test this bit
            if (workWeekInstance.startDate.dayOfWeek() != DateTimeConstants.MONDAY) {
                def removeDays = workWeekInstance.startDate.getDayOfWeek() - DateTimeConstants.MONDAY
                workWeekInstance.startDate = workWeekInstance.startDate.minusDays(removeDays)
            }

            workWeekInstance.monday.workDay = workWeekInstance.startDate
            workWeekInstance.tuesday.workDay = workWeekInstance.startDate.plusDays(1)
            workWeekInstance.wednesday.workDay = workWeekInstance.startDate.plusDays(2)
            workWeekInstance.thursday.workDay = workWeekInstance.startDate.plusDays(3)
            workWeekInstance.friday.workDay = workWeekInstance.startDate.plusDays(4)
        }

        if (workWeekInstance.save(flush: true)) {
            Contractor contractor
            if (params.contractorId) {
                contractor = Contractor.get(params.contractorId).addToWorkWeeks(workWeekInstance).save()
            }

            flash.message = "${message(code: 'default.created.message', args: [message(code: 'workWeek.label', default: 'WorkWeek'), workWeekInstance.id])}"
            redirect(uri: "/timesheet/${contractor.name.replace(" ", '-')}")
        }
        else {
            render(view: "create", model: [workWeekInstance: workWeekInstance])
        }
    }

    def view = {
        def workWeekInstance = WorkWeek.get(params.id)
        def contractor = Contractor.get(params.contractorId)
        render(template: "/invoice/invoicePdf", model: [workWeekInstance: workWeekInstance, contractor: contractor])
    }

    def pdf = {
        def workWeekInstance = WorkWeek.get(params.id)
        def contractor = Contractor.get(params.contractorId)
        if (workWeekInstance && contractor) {
            renderPdf(template: "/invoice/invoicePdf", model: [workWeekInstance: workWeekInstance, contractor: contractor], filename: "timesheet-${contractor.name}-${workWeekInstance.startDate.toString()}.pdf")
        } else {
            render 'yo we dont have everything'
        }
    }


    def sendInvoiceEmail = {
        def workWeekInstance = WorkWeek.get(params.id)
        def contractor = Contractor.get(params.contractorId)
        // Get the bytes
        def bytes = pdfRenderingService.render(template: "/invoice/invoicePdf", model: [workWeekInstance: workWeekInstance, contractor: contractor])
        println bytes.class
        sendMail {
            multipart true
            to 'glenn.saqui@gmail.com'
            subject "The issue you watch has been updated"
            body "Hello Watcher!"
            attachBytes "timesheet-${contractor.name}-${workWeekInstance.startDate.toString()}.pdf", "application/pdf", bytes.toByteArray()
        }
        render 'yo that shit has been sent'
    }


    def delete = {
        def workWeekInstance = WorkWeek.get(params.id)
        def contractor = Contractor.get(params.contractorId)

        if (workWeekInstance) {
            try {
                contractor.removeFromWorkWeeks workWeekInstance
                contractor.save()
                workWeekInstance.delete(flush: true)

                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'workWeek.label', default: 'WorkWeek'), params.id])}"
                redirect(uri: "/timesheet/${contractor.name.replace(" ", '-')}")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'workWeek.label', default: 'WorkWeek'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'workWeek.label', default: 'WorkWeek'), params.id])}"
            redirect(action: "list")
        }
    }
}
